package pearson.automation.forms;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import framework.BaseForm;
import framework.elements.Label;

public class PreparedWikiPage extends BaseForm {

    private static final Label lists = new Label(
            By.id("Document_structure_elements"), "Document structure elements");
    private static ArrayList<String> preparedChapterInnerHtml = null;

    public static final ArrayList<String> getPreparedChapterInnerHtml() {
        if (preparedChapterInnerHtml == null) {
            initChapters();
        }
        return preparedChapterInnerHtml;
    }

    private static void initChapters() {
        java.io.BufferedReader br = null;
        try {
            br = new BufferedReader(new java.io.FileReader(
                    "ChaptherInnerHTMLList.txt"));
            preparedChapterInnerHtml = new ArrayList<String>();
            while (br.ready()) {
                preparedChapterInnerHtml.add(br.readLine());
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public PreparedWikiPage() {
        super(lists.getLocator(), "Prepared page with tags");
        if (preparedChapterInnerHtml == null) {
            initChapters();
        }
    }

    public void copyPreparedText() {
        lists.click();
        /*
         * Actions builder = new Actions((ChromeDriver)browser.getDriver());
         * builder.keyDown(first, Keys.SHIFT).click(second); builder.perform();
         */
    
        String script = "var range = document.createRange();"
            + "var start = document.getElementById('Document_structure_elements');"
            + "var end = document.getElementById('Document_head_elements');"
            + "range.setStart(start);"
            + "range.setEnd(end);"
            + "window.getSelection().addRange(range);";
        browser.getDriver().executeScript(script);
        WebElement elem = browser.getDriver().switchTo().activeElement();
        elem.sendKeys(Keys.chord(Keys.LEFT_CONTROL, Keys.INSERT));
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
